package com.wnxy.admin.interceptor;

import cn.hutool.jwt.JWTUtil;
import com.wnxy.admin.common.util.TokenUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
import org.springframework.util.StringUtils;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * 跟AOP一样，底层都是：动态代理
 * @author userpu
 */

public class LoginInterceptor implements HandlerInterceptor {
    @Value("${jwt.signature}")
    private String signature;

//    @Override
//    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
//        String token = request.getHeader("Authorization");
//        if(!StringUtils.hasLength(token)){
//            //没有被认证
//            response.setStatus(HttpStatus.UNAUTHORIZED.value());//401
//            return false;
//        }
//        token = token.replace("Bearer ", "");
//        if(!JWTUtil.verify(token,signature.getBytes())){
//            response.setStatus(HttpStatus.UNAUTHORIZED.value());//401
//            return false;
//        };
//        /*判断IP地址是否&令牌中的IP地址一致，不一致，则是非法令牌*/
//        /*判断令牌是否过期*/
//        String exp = TokenUtil.getPayLoad(token, "exp");
//        LocalDateTime date = LocalDateTime.parse(exp, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
//        //过期
//        if(date.isBefore(LocalDateTime.now())){
//            //被阻止
//            response.setStatus(HttpStatus.FORBIDDEN.value());//403
//            return false;
//        }
//        return true;
//    }
}
